//
//  ZPHomePageViewController.m
//  ZPIOSEducation
//
//  Created by mini1 on 2016/11/23.
//  Copyright © 2016年 mini1. All rights reserved.
//

#import "ZPHomePageViewController.h"
#import "ZPAdvertiseViewController.h"
#import "HomeTitleCell.h"
#import "HomeCourseCell.h"
#import "HomeWorkRemindCell.h"
#import "HomeCommunityCell.h"
#import "SelectCityViewController.h"
#import "SearchViewController.h"
#import "CourseListViewController.h"
#import "HomeWorkListViewController.h"
#import "ZPMineClassViewController.h"
#import "ZPMineQRCodeViewController.h"
#import "ZPMineChangeStuViewController.h"
#import "ZPCircleScrollView.h"
#import "MessageViewController.h"
#import "ZPBaseNavigationController.h"
#import "ZPCourseViewController.h"
#import "ZPCommunityViewController.h"
#import "ZPRegisterLoginViewController.h"

#import "MJRefresh.h"

#define CircleScrollViewTag    1000
#define FunctionImageViewTag   2000
#define FunctionLabelTag       3000
#define FunctionButtonTag      4000

NSString  *existUnreadMsg = @"0";

@interface ZPHomePageViewController () <UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate,CircleScrollViewDelegate>
{
    NSMutableArray  *defaultIconArr;
    NSMutableDictionary  *topDataDic;
    NSMutableArray  *cityArray;
    NSMutableArray  *dataArray;
    
    UIWebView  *cellWebView;
    
}

@property (weak, nonatomic) IBOutlet UIButton *cityBtn;
@property (weak, nonatomic) IBOutlet UIButton *messageBtn;
@property (weak, nonatomic) IBOutlet UITableView *mainTableView;

@end

@implementation ZPHomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"首页";
    [[NSNotificationCenter  defaultCenter] addObserver:self selector:@selector(logOnNotification) name:@"logOnNotification" object:nil];
    [[NSNotificationCenter  defaultCenter] addObserver:self selector:@selector(logOutNotification) name:@"logOutNotification" object:nil];
    [[NSNotificationCenter  defaultCenter] addObserver:self selector:@selector(selectCityNotification) name:@"selectCityNotification" object:nil];
    [[NSNotificationCenter  defaultCenter] addObserver:self selector:@selector(refreshCellWebView) name:@"refreshCellWebView" object:nil];
    [[NSNotificationCenter  defaultCenter] addObserver:self selector:@selector(checkExistUnReadMessage) name:@"checkExistUnReadMessage" object:nil];
    
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    
    topDataDic = [[NSMutableDictionary  alloc] init];
    defaultIconArr = [[NSMutableArray  alloc] init];
    cityArray = [[NSMutableArray  alloc] init];
    dataArray = [[NSMutableArray  alloc] init];
    
    cellWebView = [[UIWebView  alloc] initWithFrame:CGRectMake(3, 0, SCREENWIDTH - 6, 214)];
    cellWebView.delegate = self;
    cellWebView.scrollView.scrollEnabled = NO;
    
    [self  refreshCityButtonTitle];
    
    NSArray  *imageNameArr = [NSArray  arrayWithObjects:@"Home_KeBiao",@"Home_ZuoYe",@"myClass",@"switchStudent",@"QRcode", nil];
    NSArray  *titleArr = [NSArray  arrayWithObjects:@"课表",@"作业",@"班级",@"切换学生",@"二维码", nil];
    NSInteger  count = [imageNameArr  count];
    for (int i = 0; i < count; i ++) {
        NSDictionary  *tempDic = [NSDictionary  dictionaryWithObjectsAndKeys:[imageNameArr  objectAtIndex:i],@"imgUrl",[titleArr  objectAtIndex:i],@"name", nil];
        [defaultIconArr  addObject:tempDic];
    }
    [topDataDic  setObject:defaultIconArr forKey:@"icon"];
    
    [self  refreshTableHeaderView];
    
    __weak ZPHomePageViewController *weakSelf = self;
    [self.mainTableView  addLegendHeaderWithRefreshingBlock:^{
        [weakSelf  requetIsExistMessage];
        [weakSelf  requestData];
    }];
    
    [self  requetIsExistMessage];
    [self  requestData];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)refreshTableHeaderView
{
    if (topDataDic.count == 0) {
        return ;
    }
    
    UIView  *tempView = [[UIView  alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 200)];
    tempView.backgroundColor = [UIColor  whiteColor];
    
    //图片比例  375:117
    CGFloat  scrollWidth = 0.;
    CGFloat  scrollHeight = 0.;
    NSArray  *carouselArr = [topDataDic  objectForKey:@"carousel"];
    if (carouselArr.count == 0) {
        
    }else {
        scrollWidth = SCREENWIDTH;
        scrollHeight = SCREENWIDTH * 117 / 375;
        
        ZPCircleScrollView  *circleScrollView = [[ZPCircleScrollView  alloc] initWithFrame:CGRectMake(0, 0, scrollWidth, scrollHeight)];
        circleScrollView.tag = CircleScrollViewTag;
        circleScrollView.cirScrollViewDelegate = self;
        [circleScrollView  reloadViewWithArray:carouselArr];
        [tempView  addSubview:circleScrollView];
    }
    
    CGFloat  lastLabelY = scrollHeight;
    NSArray  *iconArr = [topDataDic  objectForKey:@"icon"];
    NSInteger  count = [iconArr  count];
    
    CGFloat margin = 20.;
    if (SCREENWIDTH > 320) {
        margin = 27.;
    }
    
    CGFloat  space = (SCREENWIDTH - margin * 2 - 36 * 5) / 4;
    for (int i = 0; i < count; i++) {
        UIImageView  *imageView = [[UIImageView  alloc] initWithFrame:CGRectMake(margin + (space + 36) * (i % 5), scrollHeight + 12 + 80 * (i / 5), 36, 36)];
        NSString  *imgUrlStr = [[iconArr  objectAtIndex:i] objectForKey:@"imgUrl"];
        if (i < 5) {
            imageView.image = [UIImage  imageNamed:imgUrlStr];
        }else {
            [imageView  sd_setImageWithURL:[NSURL  URLWithString:imgUrlStr] placeholderImage:[UIImage  imageNamed:@"Placeholder355_145"]];
        }
        imageView.tag = FunctionImageViewTag + i;
        [tempView  addSubview:imageView];
        
        CGFloat  labWidth = 36 + space;
        UILabel  *titleLab = [[UILabel  alloc] initWithFrame:CGRectMake(imageView.frame.origin.x - space / 2, imageView.frame.origin.y + 36 + 8, labWidth, 15)];
        titleLab.text = [NSString  stringWithFormat:@"%@",[[iconArr  objectAtIndex:i] objectForKey:@"name"]];
        titleLab.textColor = RGBACOLOR(153, 153, 153, 1.);
        titleLab.font = [UIFont  systemFontOfSize:11.];
        titleLab.backgroundColor = [UIColor  clearColor];
        titleLab.textAlignment = NSTextAlignmentCenter;
        titleLab.tag = FunctionLabelTag + i;
        [tempView  addSubview:titleLab];
        
        UIButton  *functionBtn = [UIButton  buttonWithType:UIButtonTypeCustom];
        functionBtn.frame = CGRectMake(titleLab.frame.origin.x, imageView.frame.origin.y, titleLab.frame.size.width, 36 + 8 + 15);
        [functionBtn  setBackgroundColor:[UIColor  clearColor]];
        functionBtn.tag =  FunctionButtonTag + i;
        [functionBtn  addTarget:self action:@selector(functionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [tempView  addSubview:functionBtn];
        
        if (i == count - 1) {
            lastLabelY = titleLab.frame.origin.y;
        }
        
    }
    
    tempView.frame = CGRectMake(0, 0, SCREENWIDTH, lastLabelY + 15 + 8);
    
    self.mainTableView.tableHeaderView = tempView;
    
}

- (void)pushToCourseList
{
    CourseListViewController  *viewController = [[CourseListViewController  alloc] initWithNibName:@"CourseListViewController" bundle:nil];
    viewController.comeFromType = comefromHomePage;
    [self.navigationController  pushViewController:viewController animated:YES];
}

- (void)pushToHomeWorkList
{
    HomeWorkListViewController  *viewController = [[HomeWorkListViewController  alloc] initWithNibName:@"HomeWorkListViewController" bundle:nil];
    [self.navigationController  pushViewController:viewController animated:YES];
}

#pragma mark - Notifications
#pragma mark 登录
- (void)logOnNotification
{
    [self  requetIsExistMessage];
    [self  requestData];
}

#pragma mark 退出登录
- (void)logOutNotification
{
    existUnreadMsg = @"0";
    [self  refreshMessageBtnImage];
    [[NSNotificationCenter  defaultCenter] postNotificationName:@"changeMessageIconStatus" object:nil];
    [self  requestData];
}

#pragma mark 是否有新消息
- (void)checkExistUnReadMessage
{
    [self  requetIsExistMessage];
}

#pragma mark 选择城市
- (void)selectCityNotification
{
    [self  refreshCityButtonTitle];
}

- (void)refreshCityButtonTitle
{
    NSString  *cityName = [[ZPAuthStore  sharedZPAuthStore].userToDic  objectForKey:@"cityName"];
    if (cityName && ![cityName  isEqual:[NSNull  null]]) {
        [self.cityBtn  setTitle:[NSString  stringWithFormat:@"%@",cityName] forState:UIControlStateNormal];
        CGFloat labelWidth = [self.cityBtn.titleLabel.text sizeWithFont:self.cityBtn.titleLabel.font].width;
        [self.cityBtn  setTitleEdgeInsets:UIEdgeInsetsMake(0, -18, 0, 18)];
        [self.cityBtn  setImageEdgeInsets:UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth)];
    }
}

- (void)refreshMessageBtnImage
{
    if ([existUnreadMsg  integerValue] == 1) {
        [self.messageBtn  setImage:[UIImage  imageNamed:@"Message_RedPoint"] forState:UIControlStateNormal];
    }else {
        [self.messageBtn  setImage:[UIImage  imageNamed:@"zhenpin_un_news"] forState:UIControlStateNormal];
    }
}

- (void)refreshCellWebView
{
    [cellWebView  reload];
}

#pragma mark - Actions
#pragma mark 切换城市
- (IBAction)navCityButtonClick:(id)sender
{
    SelectCityViewController  *viewController = [[SelectCityViewController  alloc] initWithNibName:@"SelectCityViewController" bundle:nil];
    viewController.dataArray = cityArray;
    [self.navigationController  pushViewController:viewController animated:YES];
}

#pragma mark 消息中心
- (IBAction)navMessageButtonClick:(id)sender
{
    if ([[ZPAuthStore  sharedZPAuthStore] isLogin]) {
        MessageViewController  *viewController = [[MessageViewController  alloc] initWithNibName:@"MessageViewController" bundle:nil];
        [self.navigationController  pushViewController:viewController animated:YES];
    }else {
        ZPRegisterLoginViewController  *viewController = [[ZPRegisterLoginViewController  alloc] initWithNibName:@"ZPRegisterLoginViewController" bundle:nil];
        ZPBaseNavigationController  *navController = [[ZPBaseNavigationController  alloc] initWithRootViewController:viewController];
        [self presentViewController:navController animated:YES completion:nil];
    }
}

#pragma mark 搜索按钮点击
- (IBAction)searchButtonClick:(id)sender
{
    SearchViewController  *viewController = [[SearchViewController  alloc] initWithNibName:@"SearchViewController" bundle:nil];
    ZPBaseNavigationController  *navController = [[ZPBaseNavigationController  alloc] initWithRootViewController:viewController];
    [self presentViewController:navController animated:YES completion:nil];
}

#pragma mark 选课 课表 作业 问答 语文按钮点击
- (void)functionButtonClick:(id)sender
{
    UIButton  *tempBtn = (UIButton  *)sender;
    NSInteger  tag = tempBtn.tag - FunctionButtonTag;
    if (tag > 4) {
        NSArray  *iconArr = [topDataDic  objectForKey:@"icon"];
        NSDictionary  *tempDic = [iconArr  objectAtIndex:tag];
        NSInteger  type = [[tempDic  objectForKey:@"type"] integerValue];
        if (type == 1) {
            ZPCommunityViewController  *viewController = [[ZPCommunityViewController  alloc] initWithNibName:@"ZPCommunityViewController" bundle:nil];
            viewController.urlStr = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"url"]];
            viewController.titleStr = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"name"]];
            viewController.isPush = YES;
            [self.navigationController  pushViewController:viewController animated:YES];
        }else if (type == 2) {
            ZPCourseViewController  *viewController = [[ZPCourseViewController  alloc] initWithNibName:@"ZPCourseViewController" bundle:nil];
            NSString  *keyword = [tempDic  objectForKey:@"keyword"];
            if (keyword && ![keyword isEqual:[NSNull  null]]) {
                viewController.searchText = [NSString  stringWithFormat:@"%@",keyword];
            }else {
                viewController.searchText = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"name"]];
            }
            viewController.fromSearch = YES;
            [self.navigationController  pushViewController:viewController animated:YES];
        }
    }else {
        if ([[ZPAuthStore  sharedZPAuthStore] isLogin]) {
            switch (tag) {
                case 0:
                    //课表
                    [self  pushToCourseList];
                    
                    break;
                case 1:
                    //作业
                    [self  pushToHomeWorkList];
                    
                    break;
                case 2:
                    //班级
                {
                    ZPMineClassViewController  *viewController = [[ZPMineClassViewController  alloc] init];
                    [self.navigationController  pushViewController:viewController animated:YES];
                }
                    
                    break;
                case 3:
                    //切换学生
                {
                    ZPMineChangeStuViewController  *viewController = [[ZPMineChangeStuViewController  alloc] initWithNibName:@"ZPMineChangeStuViewController" bundle:nil];
                    [self.navigationController  pushViewController:viewController animated:YES];
                }
                    
                    break;
                case 4:
                    //二维码
                {
                    ZPMineQRCodeViewController  *viewController = [[ZPMineQRCodeViewController  alloc] initWithNibName:@"ZPMineQRCodeViewController" bundle:nil];
                    [self.navigationController  pushViewController:viewController animated:YES];
                }
                    
                    break;
                    
                default:
                    break;
            }
        }else {
            ZPRegisterLoginViewController  *viewController = [[ZPRegisterLoginViewController  alloc] initWithNibName:@"ZPRegisterLoginViewController" bundle:nil];
            ZPBaseNavigationController  *navController = [[ZPBaseNavigationController  alloc] initWithRootViewController:viewController];
            [self presentViewController:navController animated:YES completion:nil];
        }
    }
}

#pragma mark - CircleScrollView Delegate 
#pragma mark 轮播图图片点击
- (void)circleImageDidClick:(NSInteger)index
{
    NSArray  *carouselArr = [topDataDic  objectForKey:@"carousel"];
    NSDictionary  *tempDic = [carouselArr  objectAtIndex:index];
    NSInteger  type = [[tempDic  objectForKey:@"type"] integerValue];
    if (type == 1) {
        ZPCommunityViewController  *viewController = [[ZPCommunityViewController  alloc] initWithNibName:@"ZPCommunityViewController" bundle:nil];
        viewController.urlStr = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"url"]];
        viewController.titleStr = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"name"]];
        viewController.isPush = YES;
        [self.navigationController  pushViewController:viewController animated:YES];
    }else if (type == 2) {
        ZPCourseViewController  *viewController = [[ZPCourseViewController  alloc] initWithNibName:@"ZPCourseViewController" bundle:nil];
        NSString  *keyword = [tempDic  objectForKey:@"keyword"];
        if (keyword && ![keyword isEqual:[NSNull  null]]) {
            viewController.searchText = [NSString  stringWithFormat:@"%@",keyword];
        }else {
            viewController.searchText = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"name"]];
        }
        viewController.fromSearch = YES;
        [self.navigationController  pushViewController:viewController animated:YES];
    }
}

#pragma mark - 网络请求
#pragma mark 轮播 icon 消息
- (void)reloadButtonAction
{
    [self  requestData];
}

- (void)requestData
{
    [self  requestTopData];
    [self  requestMainData];
}

#pragma mark 是否存在未读消息
- (void)requetIsExistMessage
{
    if ([[ZPAuthStore  sharedZPAuthStore] isLogin]) {
        NSDictionary  *paramDic = [NSDictionary  dictionaryWithObjectsAndKeys:[[ZPAuthStore  sharedZPAuthStore].userToDic  objectForKey:@"stuId"] ,@"id", nil];
        [[ZPNetworking  sharedZPNetworking] POST:APIHome@"home/isExistMessage.json" parameters:paramDic progress:^(NSProgress * _Nullable progress) {
            
        } success:^(id  _Nonnull responseObject) {
            NSLog(@"++++++++++%@",responseObject);
            if ([[responseObject  objectForKey:@"code"] integerValue] == 200) {
                if ([[[responseObject  objectForKey:@"result"] objectForKey:@"message"] boolValue]) {
                    existUnreadMsg = @"1";
                }else {
                    existUnreadMsg = @"0";
                }
            }else {
                existUnreadMsg = @"0";
            }
            [self  refreshMessageBtnImage];
            [[NSNotificationCenter  defaultCenter] postNotificationName:@"changeMessageIconStatus" object:nil];
        } failure:^(NSError * _Nonnull error) {
            existUnreadMsg = @"0";
            [self  refreshMessageBtnImage];
            [[NSNotificationCenter  defaultCenter] postNotificationName:@"changeMessageIconStatus" object:nil];
        }];
    }
}

- (void)requestTopData
{
    [self  showHudithString:@"加载中..."];
    NSDictionary  *paramDic = nil;
    if ([[ZPAuthStore  sharedZPAuthStore]  isLogin]) {
        paramDic = [NSDictionary  dictionaryWithObjectsAndKeys:[[ZPAuthStore  sharedZPAuthStore].userToDic  objectForKey:@"stuId"] ,@"id", nil];
    }
    [[ZPNetworking  sharedZPNetworking] POST:APIHome@"home/homepage.json" parameters:paramDic progress:^(NSProgress * _Nullable progress) {
        
    } success:^(id  _Nonnull responseObject) {
        NSLog(@"++++++++++%@",responseObject);
        [self  hiddenHud];
        
        if ([[responseObject  objectForKey:@"code"] integerValue] == 200) {
            [topDataDic  removeAllObjects];
            [topDataDic  addEntriesFromDictionary:[responseObject  objectForKey:@"result"]];
            
            NSMutableArray  *iconArr = [[NSMutableArray  alloc] init];
            [iconArr  addObjectsFromArray:defaultIconArr];
            if ([[topDataDic  objectForKey:@"icon"] isKindOfClass:[NSArray  class]]) {
                [iconArr  addObjectsFromArray:[topDataDic  objectForKey:@"icon"]];
            }
            
            [topDataDic  setObject:iconArr forKey:@"icon"];
            [self  refreshTableHeaderView];
            
        }else {
            [ZPPromptView  showMessage:[responseObject  objectForKey:@"codeMsg"]];
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self  hiddenHud];
        
    }];
}

#pragma mark 城市 社区 课表 作业
- (void)requestMainData
{
    [self  showHudithString:@"加载中..."];
    NSDictionary  *paramDic = nil;
    if ([[ZPAuthStore  sharedZPAuthStore] isLogin]) {
        paramDic = [NSDictionary  dictionaryWithObjectsAndKeys:[[ZPAuthStore  sharedZPAuthStore].userToDic  objectForKey:@"memberId"],@"memberId",[[ZPAuthStore  sharedZPAuthStore].userToDic  objectForKey:@"stuId"],@"id", nil];
    }
    
    [[ZPNetworking  sharedZPNetworking] POST:APIHome@"home/baseInfo.json" parameters:paramDic progress:^(NSProgress * _Nullable progress) {
        
    } success:^(id  _Nonnull responseObject) {
        NSLog(@"========%@",responseObject);
        [self  hiddenHud];
        if (self.mainTableView.header.isRefreshing) {
            [self.mainTableView.header  endRefreshing];
        }
        
        if ([[responseObject  objectForKey:@"code"] integerValue] == 200) {
            NSURLRequest  *request = [NSURLRequest  requestWithURL:[NSURL  URLWithString:CommunityH5HomeUrl]];
            [cellWebView  loadRequest:request];
            
            [dataArray  removeAllObjects];
            NSDictionary  *resultDic = [responseObject  objectForKey:@"result"];
            //城市
            NSArray  *city = [resultDic objectForKey:@"city"];
            if ([city  isKindOfClass:[NSArray  class]] && city.count > 0) {
                [cityArray  removeAllObjects];
                [cityArray  addObjectsFromArray:city];
                
                NSString  *cityName = [[ZPAuthStore  sharedZPAuthStore].userToDic  objectForKey:@"cityName"];
                if (cityName && ![cityName  isEqual:[NSNull  null]] && ![cityName  isEqual:@""]) {
                    
                }else {
                    NSDictionary  *tempDic = [cityArray  objectAtIndex:0];
                    NSString *homePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/UserInfo.plist"];
                    NSMutableDictionary *dataDic = [[NSDictionary dictionaryWithContentsOfFile:homePath] mutableCopy];
                    [dataDic  setObject:[NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"name"]] forKey:@"cityName"];
                    [dataDic  setObject:[NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"selectId"]] forKey:@"cityId"];
                    [dataDic  writeToFile:homePath atomically:YES];
                    [[ZPAuthStore  sharedZPAuthStore] resetUserInfoDictionary];
                    [[ZPAuthStore  sharedZPAuthStore] setWebViewCookiesWithType:1];
                    
                    [self  refreshCityButtonTitle];
                }
            }
            
            //课程提醒
            NSArray  *schedule = [resultDic objectForKey:@"schedule"];
            if ([schedule  isKindOfClass:[NSArray  class]] && schedule.count > 0) {
                NSString  *scheduleText = [resultDic objectForKey:@"scheduleText"];
                NSString  *scheduleTextStr = @"";
                if (scheduleText && ![scheduleText  isEqual:[NSNull  null]]) {
                    scheduleTextStr = scheduleText;
                }
                [dataArray  addObject:[NSDictionary  dictionaryWithObjectsAndKeys:schedule,@"data",@"1",@"type",scheduleTextStr,@"remindText", nil]];
            }
            //作业提醒
            NSArray  *homework = [resultDic objectForKey:@"homework"];
            if ([homework  isKindOfClass:[NSArray  class]] && homework.count > 0) {
                NSString  *homeworkText = [resultDic objectForKey:@"homeworkText"];
                NSString  *homeworkTextStr = @"";
                if (homeworkText && ![homeworkText  isEqual:[NSNull  null]]) {
                    homeworkTextStr = homeworkText;
                }
                
                [dataArray  addObject:[NSDictionary  dictionaryWithObjectsAndKeys:homework,@"data",@"2",@"type",homeworkTextStr,@"remindText", nil]];
            }
            
            //社区
            [dataArray  addObject:[NSDictionary  dictionaryWithObjectsAndKeys:[[NSArray  alloc] initWithObjects:@"1", nil],@"data",@"3",@"type",@"",@"remindText", nil]];
            
            [self.mainTableView  reloadData];
        }else {
            [ZPPromptView  showMessage:[responseObject  objectForKey:@"codeMsg"]];
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self  hiddenHud];
        [self  showNoNetWorkView];
        noNetWorkView.frame = CGRectMake(noNetWorkView.frame.origin.x, 64, noNetWorkView.frame.size.width, noNetWorkView.frame.size.height);
        if (self.mainTableView.header.isRefreshing) {
            [self.mainTableView.header  endRefreshing];
        }
        
    }];
}

#pragma mark - UITableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary  *tempDic = [dataArray  objectAtIndex:section];
    return [[tempDic  objectForKey:@"data"] count] + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 8.;
}

- (UIView  *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView  *tempView = [[UIView  alloc] init];
    tempView.backgroundColor = [UIColor  clearColor];
    return tempView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger  section = [indexPath  section];
    NSInteger  row = [indexPath  row];
    if (row == 0) {
        return 40.;
    }else {
        NSDictionary  *tempDic = [dataArray  objectAtIndex:section];
        NSInteger  type = [[tempDic  objectForKey:@"type"] integerValue];
        if (type == 1) {
            //课程提醒
            return 71.;
        }else if (type == 2) {
            //作业提醒
            return 71.;
        }else if (type == 3) {
            //社区
            return cellWebView.frame.size.height;
        }
    }
    
    return 0.;
}

- (UITableViewCell  *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger  section = [indexPath  section];
    NSInteger  row = [indexPath  row];
    
    NSDictionary *sectionDic = [dataArray  objectAtIndex:section];
    NSInteger  type = [[sectionDic  objectForKey:@"type"] integerValue];
    
    if (row == 0) {
        static NSString *CellIdentifier = @"HomeTitleCell";
        HomeTitleCell  *cell = (HomeTitleCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"HomeTitleCell" owner:self options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        if (type == 1) {
            cell.detailLab.hidden = NO;
            cell.arrowImv.hidden = NO;
            cell.titleLab.text = @"课程提醒";
            cell.detailLab.text = [NSString  stringWithFormat:@"%@",[sectionDic  objectForKey:@"remindText"]];
        }else if (type == 2) {
            cell.detailLab.hidden = NO;
            cell.arrowImv.hidden = NO;
            cell.titleLab.text = @"作业提醒";
            cell.detailLab.text = [NSString  stringWithFormat:@"%@",[sectionDic  objectForKey:@"remindText"]];
        }else if (type == 3) {
            cell.detailLab.hidden = YES;
            cell.arrowImv.hidden = YES;
            cell.titleLab.text = @"社区新帖";
        }
        
        return cell;
        
    }else {
        NSDictionary  *rowDic = [[sectionDic  objectForKey:@"data"] objectAtIndex:row - 1];
        
        if (type == 1) {
            static NSString *CellIdentifier = @"HomeCourseCell";
            HomeCourseCell  *cell = (HomeCourseCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"HomeCourseCell" owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            if ([[rowDic  objectForKey:@"type"] integerValue] == 0) {
                cell.timeImv.image = [UIImage  imageNamed:@"Home_Time_White"];
                cell.locationImv.image = [UIImage  imageNamed:@"Home_Loction_White"];
                cell.bgView.backgroundColor = RGBACOLOR(249, 249, 249, 1.);
                cell.timeLab.textColor = RGBACOLOR(205, 205, 205, 1.);
                cell.addressLab.textColor = RGBACOLOR(205, 205, 205, 1.);
            }else {
                cell.timeImv.image = [UIImage  imageNamed:@"Home_Time"];
                cell.locationImv.image = [UIImage  imageNamed:@"Home_Loction"];
                cell.bgView.backgroundColor = RGBACOLOR(247, 251, 255, 1.);
                cell.timeLab.textColor = RGBACOLOR(51, 51, 51, 1.);
                cell.addressLab.textColor = RGBACOLOR(51, 51, 51, 1.);
            }
            [cell.courseImv  sd_setImageWithURL:[NSURL  URLWithString:[NSString  stringWithFormat:@"%@",[rowDic  objectForKey:@"imgUrl"]]] placeholderImage:[UIImage  imageNamed:@"Placeholder200_200"]];
            cell.timeLab.text = [NSString  stringWithFormat:@"%@  %@~%@",[rowDic  objectForKey:@"dateString"],[rowDic  objectForKey:@"startTimeString"],[rowDic  objectForKey:@"endTimeString"]];
            cell.addressLab.text = [NSString  stringWithFormat:@"%@    %@",[rowDic  objectForKey:@"teachName"],[rowDic  objectForKey:@"classroomName"]];
            
            return cell;
        }else if (type == 2) {
            static NSString *CellIdentifier = @"HomeWorkRemindCell";
            HomeWorkRemindCell  *cell = (HomeWorkRemindCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"HomeWorkRemindCell" owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            cell.titleLab.text = [NSString  stringWithFormat:@"%@  %@",[rowDic  objectForKey:@"dates"],[rowDic  objectForKey:@"courseName"]];
            if ([[rowDic  objectForKey:@"isPicture"] integerValue] == 1) {
                cell.rightImv.hidden = NO;
            }else {
                cell.rightImv.hidden = YES;
            }
            
            return cell;
        }else if (type == 3) {
            static NSString *CellIdentifier = @"HomeWebViewCell";
            UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell  alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                cell.contentView.backgroundColor = RGBACOLOR(238, 238, 238, 1.);
                [cell.contentView  addSubview:cellWebView];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            return cell;
        }
    }
    
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger  section = [indexPath  section];
    NSDictionary *sectionDic = [dataArray  objectAtIndex:section];
    NSInteger  type = [[sectionDic  objectForKey:@"type"] integerValue];
    
    if (type == 1) {
        [self  pushToCourseList];
    }else if (type == 2) {
        [self  pushToHomeWorkList];
    }else if (type == 3) {
        
    }
    
}

#pragma mark - UIWebView Delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([request.URL.absoluteString  isEqualToString:CommunityH5HomeUrl]) {
        return YES;
    }else {
        ZPCommunityViewController  *viewController = [[ZPCommunityViewController  alloc] initWithNibName:@"ZPCommunityViewController" bundle:nil];
        viewController.urlStr = request.URL.absoluteString;
        viewController.titleStr = @"";
        viewController.isPush = YES;
        [self.navigationController  pushViewController:viewController animated:YES];
        
        return NO;
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //获取到webview的高度
    CGFloat height = [[cellWebView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
    cellWebView.frame = CGRectMake(cellWebView.frame.origin.x,cellWebView.frame.origin.y, cellWebView.frame.size.width, height);
    [self.mainTableView reloadData];
}

- (void)dealloc
{
    [[NSNotificationCenter  defaultCenter] removeObserver:self name:@"logOnNotification" object:nil];
    [[NSNotificationCenter  defaultCenter] removeObserver:self name:@"logOutNotification" object:nil];
    [[NSNotificationCenter  defaultCenter] removeObserver:self name:@"selectCityNotification" object:nil];
    [[NSNotificationCenter  defaultCenter] removeObserver:self name:@"refreshCellWebView" object:nil];
    [[NSNotificationCenter  defaultCenter] removeObserver:self name:@"checkExistUnReadMessage" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
